home *** CD-ROM | disk | FTP | other *** search
/ 500 MB Nyheder Direkte fra Internet 2 / 500 MB nyheder direkte fra internet CD 2.iso / start / data / text / weird.txt < prev    next >
Text File  |  1994-09-21  |  68KB  |  1,548 lines

  1.  
  2.                    FANCY INPUT
  3.   The typical INPUT statement looks like this:
  4. 10 INPUT "WHAT IS YOUR NAME";N$
  5. It makes the computer ask ``WHAT IS YOUR NAME?'' then wait for 
  6. you to answer to question. So when you run the program, the 
  7. conversation looks like this:
  8. WHAT IS YOUR NAME? MARIA
  9.   Notice that the computer automatically adds a question mark at 
  10. the end of the question, and leaves a blank space after the 
  11. question mark.
  12.  
  13.            Omitting the question mark
  14.   If you want to omit the question mark and the blank space, 
  15. replace the semicolon by a comma:
  16. 10 INPUT "WHAT IS YOUR NAME",N$
  17. The computer will omit the question mark and the blank space, so 
  18. the conversation will look like this:
  19. WHAT IS YOUR NAMEMARIA
  20.   Here's a prettier example of how to use the comma:
  21. 10 INPUT "PLEASE TYPE YOUR NAME...",N$
  22. The conversation will look like this:
  23. PLEASE TYPE YOUR NAME...MARIA
  24.   Here's an even prettier example:
  25. 10 INPUT "TO BECOME A MOVIE STAR, TYPE YOUR NAME NEXT TO THE 
  26. STARS***";N$
  27. The conversation will look like this:
  28. TO BECOME A MOVIE STAR, TYPE YOUR NAME NEXT TO THE STARS***MARIA
  29.  
  30.                Omitting the prompt
  31.   The typical INPUT statement contains a question, such as ``WHAT 
  32. IS YOUR NAME''. The question is called the prompt. If you wish, 
  33. you can omit the prompt, like this:
  34. 10 INPUT N$
  35. That line doesn't include a question, but the computer still 
  36. prints a question mark followed by a blank space, so the 
  37. conversation looks like this:
  38. ? MARIA
  39.   To make that example more practical, add a PRINT statement 
  40. above line 10, like this:
  41. 9 PRINT "PLEASE TYPE YOUR NAME AFTER THE QUESTION MARK"
  42. 10 INPUT N$
  43. That makes the conversation look like this:
  44. PLEASE TYPE YOUR NAME AFTER THE QUESTION MARK
  45. ? MARIA
  46.  
  47.  
  48.           Adjacent printing
  49.   Here's a simple program:
  50. 10 INPUT "WHAT IS YOUR NAME";N$
  51. 20 PRINT "!!!WHAT A WONDERFUL NAME!!!"
  52. It produces this conversation:
  53. WHAT IS YOUR NAME? MARIA
  54. !!!WHAT A WONDERFUL NAME!!!
  55.   To have more fun, insert a semicolon immediately after the word 
  56. INPUT, like this:
  57. 10 INPUT;"WHAT IS YOUR NAME";N$
  58. 20 PRINT "!!!WHAT A WONDERFUL NAME!!!"
  59. The conversation will begin normally:
  60. WHAT IS YOUR NAME? MARIA
  61. But when you press the ENTER key after MARY, the extra semicolon 
  62. makes the computer print line 20 next to MARIA, like this:
  63. WHAT IS YOUR NAME? MARIA!!!WHAT A WONDERFUL NAME!!!
  64.   To surprise your friends, run this program:
  65. 10 INPUT;"WHAT IS YOUR NAME";N$
  66. 20 PRINT N$;N$;N$
  67. The program begins by asking:
  68. WHAT IS YOUR NAME?
  69. Suppose the person says MARIA, like this:
  70. WHAT IS YOUR NAME? MARIA
  71. When the person presses the ENTER key after MARIA, line 20 
  72. automatically prints MARIA three more times afterwards, like 
  73. this:
  74. WHAT IS YOUR NAME? MARIAMARIAMARIAMARIA
  75.   This program asks for your first name, then your last name:
  76. 10 INPUT;"WHAT IS YOUR FIRST NAME";F$
  77. 20 INPUT "   WHAT IS YOUR LAST NAME";L$
  78. Line 10 makes the conversation begin like this:
  79. WHAT IS YOUR FIRST NAME? MARIA
  80. When you press the ENTER key after MARIA, the extra semicolon in 
  81. line 10 makes line 20 appear on the same line, like this:
  82. WHAT IS YOUR FIRST NAME? MARIA   WHAT IS YOUR LAST NAME?
  83. If you answer WONG, the whole conversation looks like this:
  84. WHAT IS YOUR FIRST NAME? MARIA   WHAT IS YOUR LAST NAME? YEE
  85.  
  86.            Multiple input
  87.   This program asks for your name, age, and weight:
  88. 10 INPUT "NAME, AGE, WEIGHT";N$,A,W
  89.   When you run the program, the computer asks:
  90. NAME, AGE, WEIGHT?
  91.   The computer waits for you to type your name, age, and weight. 
  92. When you type them, put commas between them, like this:
  93. NAME, AGE, WEIGHT? JOHN,25,148
  94.   If your name is ``JOHN SMITH, JR.'', and you want to input all 
  95. that instead of just JOHN, you must put quotation marks around 
  96. your name:
  97. NAME, AGE, WEIGHT? "JOHN SMITH, JR.",25,148
  98. Here's the rule: you must put quotation marks around any INPUT 
  99. string that contains a comma.
  100.                                                     LINE INPUT
  101.                                          If you say ___ 
  102. 10 LINE INPUT "PLEASE TYPE YOUR NAME...";N$
  103. the computer will say:
  104. PLEASE TYPE YOUR NAME...
  105. Then the computer will wait for you to type your name. You do not 
  106. have to put quotation marks around your name, even if your name 
  107. contains a comma. LINE INPUT means: the entire line that the 
  108. person inputs will become the string, even if the line contains a 
  109. comma.
  110.                                          Notice that the LINE 
  111. INPUT statement does not make the computer automatically print a 
  112. question mark. And notice that the variable must be a string 
  113. (such as N$), not a number.
  114.  
  115.                                                       INPUT$
  116.                                          This program reveals 
  117. private information about MARY:
  118. 10 PRINT "MARY SECRETLY WISHES TO KISS A COW"
  119.                                          Here's how to protect 
  120. that program, so only people who know the ``secret password'' can 
  121. run it. . . . 
  122.                                          First, invent a secret 
  123. password. Let's make it be ``TUNA''.
  124.                                          Here's the program:
  125. 5 INPUT "WHAT'S THE SECRET PASSWORD";P$
  126. 10 IF P$="TUNA" THEN PRINT "MARY SECRETLY WISHES TO KISS A C
  127. OW" ELSE PRINT "YOU ARE AN UNAUTHORIZED USER"
  128. Line 5 asks the person to type the secret password. Whatever the 
  129. person types is called P$. If the person types TUNA, line 10 
  130. makes the computer say:
  131. MARY SECRETLY WISHES TO KISS A COW
  132. But if the person does not type TUNA, the computer says ``YOU ARE 
  133. AN UNAUTHORIZED USER'' and refuses to reveal Mary's secret 
  134. desire.
  135.                                          This program's better:
  136. 5 PRINT "PLEASE TYPE THE SECRET PASSWORD"
  137. 6 P$=INPUT$(4)
  138. 10 IF P$="TUNA" THEN PRINT "MARY SECRETLY WISHES TO KISS A C
  139. OW" ELSE PRINT "YOU ARE AN UNAUTHORIZED USER"
  140. Line 5 makes the computer say:
  141. PLEASE TYPE THE SECRET PASSWORD
  142. Line 6 waits for the person to input 4 characters. The characters 
  143. that the person inputs will become P$. For example, suppose the 
  144. person types T, then U, then N, then A; then P$ will become TUNA 
  145. and the computer will reveal Mary's secret.
  146.                                          While the person inputs 
  147. the 4 characters, they won't appear on the screen; they'll be 
  148. invisible. That's to prevent other people in the room from 
  149. peeking at the screen and noticing the password.
  150.                                          After typing the 4 
  151. characters, the person does not have to press the ENTER key. As 
  152. soon as the person types the 4th character, the computer makes P$ 
  153. be the 4 characters that the person typed.
  154.   Broken computer This devilish program makes your computer 
  155. pretend to be broken, so that whenever you press the W key your 
  156. screen shows an F instead:
  157. 10 CLS
  158. 20 A$=INPUT$(1)
  159. 30 IF A$="W" THEN PRINT "F"; ELSE PRINT A$;
  160. 40 GO TO 20
  161.   Line 10 clears the screen (makes the screen go blank), so that 
  162. your friends don't see the program. Line 20 waits for you to type 
  163. 1 character. Line 30 says: if the character you typed was W, 
  164. print an F on the screen instead; otherwise, print the character 
  165. you typed. Line 40 makes the routine repeat, forever. For 
  166. example, if you try to type ``THE WEATHER IS WONDERFUL'', you'll 
  167. see this on the screen instead:
  168. THE FEATHER IS FONDERFUL
  169.   For an even wilder time, tell the computer to change each ``E'' 
  170. to ``OOGA'', by changing line 30 to this:
  171. 30 IF A$="E" THEN PRINT "OOGA"; ELSE PRINT A$;
  172. Then if you try to type ``WE ARE HERE'', you'll see this on the 
  173. screen instead:
  174. WOOGA AROOGA HOOGAROOGA
  175.   Abridged literature This program gives you a choice of 
  176. literature:
  177. 10 PRINT "WELCOME TO THE WORLD'S GREAT LITERATURE, ABRIDGED"
  178. 20 PRINT "WHICH KIND OF LITERATURE WOULD YOU LIKE?"
  179. 30 PRINT "N: NOVEL"
  180. 40 PRINT "P: POEM"
  181. 50 PRINT "PLEASE PRESS N OR P"
  182. 60 A$=INPUT$(1)
  183. 70 IF A$="N" THEN GO TO 100
  184. 80 IF A$="P" THEN GO TO 200
  185. 90 GO TO 50
  186. 100 PRINT "HE: I LOVE YOU"
  187. 110 PRINT "SHE: I'M PREGNANT"
  188. 120 PRINT "HE: LET'S GET MARRIED"
  189. 130 PRINT "SHE: LET'S GET DIVORCED"
  190. 140 PRINT "HE: LET'S GET BACK TOGETHER"
  191. 150 PRINT "SHE: TOO BAD YOU DIED IN THE WAR, BUT I'LL NEVER 
  192. FORGET YOU!"
  193. 160 END
  194. 200 PRINT "NOSES"
  195. 210 PRINT "BLOWSES"
  196.   Lines 10-50 make the computer print:
  197. WELCOME TO THE WORLD'S GREAT LITERATURE, ABRIDGED
  198. WHICH KIND OF LITERATURE WOULD YOU LIKE?
  199. N: NOVEL
  200. P: POEM
  201. PLEASE PRESS N OR P
  202.   Line 60 makes the computer wait for you to press a key. You do 
  203. not have to press the ENTER key afterwards.
  204.   If you press the N key, line 70 sends the computer to line 100, 
  205. which prints an abridged novel. If you press the P key instead, 
  206. line 80 sends the computer to line 200, which prints an abridged 
  207. poem.
  208.   If you press neither N nor P, the computer arrives at line 90, 
  209. which sends the computer back to line 50, which reminds you to 
  210. press N or P.
  211.  
  212.         JOYSTICK VERSUS MOUSE
  213.                                          Instead of using the 
  214. keyboard, you can input by using a joystick or mouse.
  215.  
  216.                                                      Joystick
  217.                                          A joystick is a stick 
  218. that sticks up from a box. You can wiggle the stick in all four 
  219. directions (left, right, forward, and back) and diagonally.
  220.                                          In line 100 of your 
  221. program, if you want the computer to look at the joystick, say:
  222. 100 X=STICK(0): Y=STICK(1)
  223. That line makes X become a number that indicates how far the 
  224. joystick is being pulled to the right, and Y become a number that 
  225. indicates how far the joystick is being pushed forward. To make 
  226. the computer print X and Y on your screen, say:
  227. 110 PRINT X,Y
  228.                                          To make the computer 
  229. look at the joystick again and again, repeatedly, create a loop, 
  230. by adding this line:
  231. 120 GO TO 100
  232. Then as you wiggle the joystick, you can watch the numbers on the 
  233. screen change.
  234.                                          Besides printing the 
  235. numbers X and Y on the screen, you can use the numbers X and Y in 
  236. graphics statements (such as PLOT, LINE, and CIRCLE), so that the 
  237. joystick controls the location of graphics shapes on the screen. 
  238. You can also use the X and Y numbers in the SOUND statement, so 
  239. that the joystick controls the pitch of musical sounds: moving 
  240. the joystick will change the pitch.
  241.  
  242.                                                        Mouse
  243.                                          A mouse is a small box 
  244. about the size of a pack of cigarettes. You can slide the mouse 
  245. across your desk in all four directions (left, right, forward, 
  246. and back) and diagonally.
  247.                                          Some versions of BASIC 
  248. let your program contain commands to handle a mouse. Those 
  249. commands are called mouse commands.
  250.                                          (GWBASIC and QBASIC do 
  251. not let programs contain mouse commands. If you're using GWBASIC 
  252. or QBASIC, skip ahead to the next page.)
  253.                                          If your computer's BASIC 
  254. understands mouse commands, and you want the computer to look at 
  255. the mouse in line 100 of your program, say this:
  256. 100 M=MOUSE(0): X=MOUSE(1): Y=MOUSE(2)
  257. In that line, the ``M=MOUSE(0)'' makes the computer look at the 
  258. mouse. The rest of the line makes X become a number that 
  259. indicates how far the mouse was moved to the right, and Y become 
  260. a number that indicates how far the mouse was moved forward. To 
  261. make the computer print X and Y on your screen, say:
  262. 110 PRINT X,Y
  263.                                          To make the computer 
  264. look at the mouse again and again repeatedly, create a loop by 
  265. adding this line:
  266. 120 GO TO 100
  267. Then as you move the mouse across your desk, you can watch the 
  268. numbers on the screen change.
  269.  
  270.           SWAP
  271.   Modern computers understand the word SWAP:
  272. 10 A=4: B=9
  273. 20 SWAP A,B
  274. 30 PRINT A;B
  275.   Line 10 says A is 4 and B is 9. Line 20 swaps A with B, so that 
  276. A becomes 9, and B becomes 4. Line 30 prints:
  277.  9  4
  278.   If your computer is old-fashioned and doesn't understand the 
  279. word SWAP, replace line 20 by this:
  280. 20 S=A: A=B: B=S
  281.   That example swapped numbers. You can also swap strings:
  282. 10 A$="HORSE": B$="CART"
  283. 20 SWAP A$,B$
  284. 30 PRINT A$;" ";B$
  285. Line 10 says A$ is ``HORSE'' and B$ is ``CART''. Line 20 swaps A$ 
  286. with B$, so that A$ becomes ``CART'', and B$ becomes ``HORSE''. 
  287. Line 30 puts the CART before the HORSE:
  288. CART HORSE
  289. If your computer doesn't understand SWAP, replace line 20 by 
  290. this:
  291. 20 S$=A$: A$=B$: B$=S$
  292. Don't forget the dollar signs!
  293.                                                Shuffling
  294.                              Here are some cards:
  295. Queen of Hearts
  296. Jack of Diamonds
  297. Ace of Spades
  298. Joker
  299. King of Clubs
  300.                              Let's shuffle them, to put them into 
  301. a random order.
  302.                              To begin, put the list of cards into 
  303. a DATA statement:
  304. 10 DATA QUEEN OF HEARTS,JACK OF DIAMONDS,ACE OF SPADES,JOKER,KING 
  305. OF CLUBS
  306.                              We have 5 cards:
  307. 20 N=5
  308.                              Let the Queen of Hearts be called 
  309. X$(1), the Jack of Diamonds be called X$(2), the Ace of Spades be 
  310. called X$(3), the Joker be called X$(4),and the King of Clubs be 
  311. called X$(5):
  312. 30 DIM X$(N)
  313. 40 FOR I = 1 TO N: READ X$(I): NEXT
  314.                              Shuffle the cards, by using the 
  315. following strategy. . . . 
  316.                              Swap the card N with a random card 
  317. before it (or with itself); then swap card N-1 with a random card 
  318. before it (or with itself); then swap card N-2 with a random card 
  319. before it (or with itself); etc. Keep doing that, until you 
  320. finally reach card 2, which you swap with a random card before it 
  321. (or with itself). Here's the code:
  322. 50 RANDOMIZE
  323. 60 FOR I = N TO 2 STEP -1: SWAP X$(I),X$(RND(I)): NEXT
  324. If your computer doesn't understand SWAP, replace ``SWAP 
  325. X$(I),X$(RND(I))'' by this:
  326. R=RND(I): S$=X$(I): X$(I)=X$(R): X$(R)=S$
  327. If your computer doesn't understand the word RANDOMIZE, or 
  328. doesn't understand that RND(I) is a random number from 1 to I, 
  329. you must make further changes.
  330.                              Finally, print the shuffled deck:
  331. 70 FOR I = 1 TO N: PRINT X$(I): NEXT
  332.                              The computer will print something 
  333. like this:
  334. KING OF CLUBS
  335. JOKER
  336. JACK OF DIAMONDS
  337. QUEEN OF HEARTS
  338. ACE OF SPADES
  339.                              To shuffle a larger deck, change 
  340. just lines 10 and 20.
  341.                      Sorting
  342.   Putting words in alphabetical order ___ or putting numbers in 
  343. numerical order ___ is called sorting.
  344.   Short Three-Line Sort Here are a dozen names: Sue, Ann, Joe, 
  345. Alice, Ted, Jill, Fred, Al, Sam, Pat, Sally, Moe. Let's make the 
  346. computer alphabetize them (sort them).
  347.   To begin, put the list of names into a DATA statement:
  348. 10 DATA SUE,ANN,JOE,ALICE,TED,JILL,FRED,AL,SAM,PAT,SALLY,MOE
  349.   We have 12 names:
  350. 20 N=12
  351.   Let Sue be called X$(1), Ann be called X$(2), Joe be called 
  352. X$(3), etc. Here's how:
  353. 30 DIM X$(N)
  354. 40 FOR I = 1 TO N: READ X$(I): NEXT
  355.   Alphabetize the names, by using the following strategy. . . . 
  356.   Compare the first name against the second; if they're not in 
  357. alphabetical order, swap them. Compare the second name against 
  358. the third; if they're not in alphabetical order, swap them. 
  359. Compare the third name against the fourth; if they're not in 
  360. alphabetical order, swap them. Continue that process, until you 
  361. finally compare the last two names. But each time you swap, you 
  362. must start the whole process over again, to make sure the 
  363. preceding names are still in alphabetical order. Here's the code:
  364. 50 FOR I = 1 TO N-1
  365. 60     IF X$(I)>X$(I+1) THEN SWAP X$(I),X$(I+1): GO TO 50
  366. 70 NEXT
  367.   Finally, print the alphabetized list:
  368. 80 FOR I = 1 TO N: PRINT X$(I): NEXT
  369. The computer will print:
  370. AL
  371. ALICE
  372. ANN
  373. FRED
  374. JILL
  375. JOE
  376. MOE
  377. PAT
  378. SALLY
  379. SAM
  380. SUE
  381. TED
  382.   In that program, the sorting occurs in lines 50-70. Those three 
  383. short lines are called the Short Three-Line Sort.
  384.   Those three lines form a loop. The part of the loop that the 
  385. computer encounters the most often is the phrase ``IF 
  386. X$(I)>X$(I+1)''. In fact, if you tell the computer to alphabetize 
  387. a list of names that's long (several hundred names), the computer 
  388. spends the majority of its time repeatedly handling ``IF 
  389. X$(I)>X$(I+1)''.
  390.   How long will the computer take to handle a long list of names? 
  391. The length of time depends mainly on how often the computer 
  392. encounters the phrase ``IF X$(I)>X$(I+1)''.
  393.   If the list contains N names, the number of times that the 
  394. computer encounters the phrase ``IF X$(I)>X$(I+1)'' is 
  395. approximately N3/8. Here are some examples:
  396. N (number of names)N3/8 (approximate number of encounters)
  397.     10                      125
  398.     12                      216
  399.     20                    1,000
  400.     40                    8,000
  401.    100                  125,000
  402.  1,000              125,000,000
  403. 10,000          125,000,000,000
  404.   For example, the chart says that a list of 12 names requires 
  405. approximately 216 encounters.
  406.                                                      The 216 is 
  407. just an approximation: the exact number of encounters depends on 
  408. which list of names you're trying to alphabetize. If the list is 
  409. nearly in alphabetical order already, the number of encounters 
  410. will be less than 216; if the list is in reverse alphabetical 
  411. order, the number of encounters will be more than 216; but if the 
  412. list is typical (not yet in any particular order), the number of 
  413. encounters will be about 216. For the list that we tried (Sue, 
  414. Ann, Joe, Alice, Ted, Jill, Fred, Al, Sam, Pat, Sally, Moe), the 
  415. exact number of encounters happens to be 189, which is close to 
  416. 216.
  417.   How long will your computer take to finish sorting? The length 
  418. of time depends on which computer you have, how many names are in 
  419. the list, and how long each name is. A typical microcomputer 
  420. (handling a list of typical names) requires .01 seconds per 
  421. encounter. Multiplying the number of encounters by .01 seconds, 
  422. you get:
  423. Number of namesEncounters (N3/8)               Time                     
  424.     10                    125            1.25 secs
  425.     12                    216            2.16 secs
  426.     20                  1,000           10    secs
  427.     40                  8,000           80    secs =  1.3 minutes
  428.    100                125,000        1,250    secs = 20.8 minutes
  429.  1,000            125,000,000    1,250,000    secs = 14.4 days
  430. 10,000        125,000,000,0001,250,000,000    secs = 39.6 years
  431.   Moral: never let a 70-year-old programmer alphabetize a list of 
  432. 10,000 names by using the Short Three-Line Sort ___ because when 
  433. the computer finishes running the program (39.6 years later), the 
  434. programmer will probably be dead!
  435.   Fancy Three-Line Sort In the Short Three-Line Sort, the end of 
  436. line 60 says ``GO TO 50''. For an interesting experiment, replace 
  437. the ``GO TO 50'' by ``IF I>1 THEN I=I-1: GO TO 60'', so that line 
  438. 60 looks like this:
  439. 60     IF X$(I)>X$(I+1) THEN SWAP X$(I),X$(I+1): IF I>1 THEN 
  440. I=I-1: GO TO 60
  441.   Even though that new, fancy version of line 60 is longer to 
  442. type than the original, the computer handles it more quickly, 
  443. because it tells the computer to GO TO line 60 instead of forcing 
  444. the computer to go all the way back to line 50. The fancy version 
  445. is called the Fancy Three-Line Sort.
  446.   If you feed the computer the Fancy Three-Line Sort (instead of 
  447. the Short Three-Line Sort), the computer encounters the ``IF 
  448. X$(I)>X$(I+1)'' less often: just N2/2 times (instead of N3/8).
  449. Number of namesEncounters (N2/2)         Time                
  450.     10                50           .50 secs
  451.     12                72           .72 secs
  452.     20               200          2    secs
  453.     40               800          8    secs
  454.    100             5,000         50    secs
  455.  1,000           500,000      5,000    secs = 1.4 hours
  456. 10,000        50,000,000    500,000    secs = 5.8 days
  457.   Now you can hire the 70-year old programmer: to alphabetize 
  458. 10,000 names, he'll need just 5.8 days instead of 39.6 years. By 
  459. changing just the end of line 60, we saved almost 40 years of 
  460. computer time ___ and the programmer's job!
  461.   Super-Fancy Three-Line Sort To reduce the time even further, 
  462. use the Super-Fancy Three-Line Sort instead, which is:
  463. 50 FOR I = 1 TO N-1
  464. 60     FOR J = I TO 1 STEP -1: IF X$(J)>X$(J+1) THEN SWAP 
  465. X$(J),X$(J+1): NEXT
  466. 70 NEXT I
  467.   When typing the Super-Fancy Three-Line Sort, make sure you type 
  468. line 60 correctly: you must put the IF on the same line as the 
  469. NEXT. In line 70, make sure you put the ``I'' after NEXT; 
  470. otherwise, the computer might think you mean NEXT J.
  471.   For the Super-Fancy Three-Line Sort, the number of times the 
  472. computer encounters the phrase ``IF X$(J)>X$(J+1)'' is just N2/4:
  473. Number of namesEncounters (N2/4)         Time                
  474.     10                25           .25 secs
  475.     12                36           .36 secs
  476.     20               100          1    secs
  477.     40               400          4    secs
  478.    100             2,500         25    secs
  479.  1,000           250,000      2,500    secs = 41.7 minutes
  480. 10,000        25,000,000    250,000    secs =  2.9 days
  481.   Six-Line Sort The Six-Line Sort reduces the time even further.
  482.   To construct the Six-Line Sort, begin with the Super-Fancy 
  483. Three-Line Sort, but change each +1 to +D, and change each -1 to 
  484. -D, so that you get:
  485. 50 FOR I = 1 TO N-D
  486. 60     FOR J = I TO 1 STEP -D: IF X$(J)>X$(J+D) THEN SWAP 
  487. X$(J),X$(J+D): NEXT
  488. 70 NEXT I
  489.  
  490.                                                      D begins by 
  491. being N ___ 
  492. 42 D=N
  493. but then decreases, so that it becomes a fifth of its original 
  494. value:
  495. 44 D=INT(D/5)+1
  496. After performing lines 50-70 for that D, check whether D is down 
  497. to 1 yet; if it isn't, repeat the process:
  498. 75 IF D>1 THEN GO TO 44
  499.  
  500.   So altogether, the complete Six-Line Sort looks like this:
  501. 42 D=N
  502. 44 D=INT(D/5)+1
  503. 50 FOR I = 1 TO N-D
  504. 60     FOR J = I TO 1 STEP -D: IF X$(J)>X$(J+D) THEN SWAP 
  505. X$(J),X$(J+D): NEXT
  506. 70 NEXT I
  507. 75 IF D>1 THEN GO TO 44
  508.   For the Six-Line Sort, the number of times the computer 
  509. encounters the phrase ``IF X$(J)>X$(J+D)'' is just 
  510. 1.5*(N-1)^(4/3):
  511. Number of namesEncounters      Time                    
  512.     10             28        .28 secs
  513.     12             37        .37 secs
  514.     20             76        .76 secs
  515.     40            198       1.98 secs
  516.    100            687       6.87 secs
  517.  1,000         14,980     149.80 secs =  2.5 minutes
  518. 10,000        323,122   3,231.22 secs = 53.9 minutes
  519.   Notice that the Six-Line Sort handles 10,000 names in 53.9 
  520. minutes. That's much faster than the Super-Fancy Three-Line Sort, 
  521. which takes 2.9 days. But to handle just 10 names, the Six-Line 
  522. Sort does not run faster than the Super-Fancy Three-Line Sort. So 
  523. use the Six-Line Sort just for handling long lists of names.
  524.   To make the Six-Line Sort even faster, add this line:
  525. 1 DEFINT A-Z
  526. That tells the computer that none of the variables stands for a 
  527. decimal. By saying DEFINT A-Z, you enable the computer to run the 
  528. program 20% faster, so that the timing looks like this:
  529. Number of names      Time                
  530.     10             .2 secs
  531.     12             .3 secs
  532.     20             .6 secs
  533.     40            1.6 secs
  534.    100            5.5 secs
  535.  1,000          120   secs =  2 minutes
  536. 10,000        2,580   secs = 43 minutes
  537.   We've sure come a long way! Our first attempt (the Short 
  538. Three-Line Sort) took 39.6 years to alphabetize 10,000 names; our 
  539. newest attempt (the Six-Line Sort with DEFINT) takes just 43 
  540. minutes.
  541.   If you try running those sorting methods on your computer, 
  542. you'll find the timings are slightly different, since the exact 
  543. timings depend on which computer you have, the length of each 
  544. name, and how badly the names are out of order.
  545.   Famous sorts Although I ``invented'' all those sorting methods, 
  546. most of them are just slight improvements on methods that were 
  547. developed by others. For example, the Super-Fancy Three-Line Sort 
  548. is a slight improvement on the Shuttle Sort, which was invented 
  549. by Shaw & Trimble in 1983. The Six-Line Sort is a slight 
  550. improvement on the Shell Sort, which was invented by Donald Shell 
  551. in 1959 and further developed by Hibbard & Boothroyd in 1963, 
  552. Peterson & Russell in 1971, and Knuth in 1973.
  553.   Phone directory Suppose you want to alphabetize this phone 
  554. directory:
  555. Name    Phone number
  556. Mary Smith277-8139
  557. John Doe513-9134
  558. Russ Walter666-2666
  559. Information555-1212
  560.   Just use one of the alphabetizing programs I showed you! Type 
  561. the DATA like this:
  562. 10 DATA SMITH MARY 277-8139,DOE JOHN 513-9134
  563. 20 DATA WALTER RUSS 666-2666,INFORMATION 555-1212
  564. The computer will print:
  565. DOE JOHN 513-9134
  566. INFORMATION 555-1212
  567. SMITH MARY 277-8139
  568. WALTER RUSS 666-2666
  569.  
  570.                                                      Sorting 
  571. numbers Suppose you want to put a list of numbers into increasing 
  572. order. For example, if the numbers are 51, 4.257, -814, 62, and 
  573. .2, let's make the computer print:
  574. -814
  575.   .2
  576.  4.257
  577.  51
  578.  62
  579.                                                      To do that, 
  580. just use one of the alphabetizing programs I showed you ___ but 
  581. in the DATA statement, put the numbers instead of strings; and 
  582. remove the dollar signs (say X instead of X$).
  583.                                                      To put a 
  584. list of numbers into decreasing order, begin by writing a program 
  585. that puts them into increasing order, and then change line 60 
  586. from ``>X'' to ``<X''.
  587.  
  588.            ON
  589.   The computer understands the word ``ON''.
  590.  
  591.      ON . . . GO TO
  592.   In your program, you can say:
  593. 50 ON N GO TO 80,100,20,350
  594. That means: go to either 80, 100, 20, or 350; the decision 
  595. depends on what N is. In other words:
  596. If N is 1, go to 80.
  597. If N is 2, go to 100.
  598. If N is 3, go to 20.
  599. If N is 4, go to 350.
  600.   In that example, if N is not 1, 2, 3, or 4, the computer will 
  601. not go to line 80 or 100 or 20 or 350; instead, the computer will 
  602. proceed to the line underneath line 50 (which is probably line 
  603. 60).
  604.   Exception: if N is greater than 255 or a negative number (such 
  605. as -1), the computer thinks you're crazy, and so the computer 
  606. gripes at you instead of proceeding to line 60.
  607.   Another exception: if N is a decimal (such as 3.1), the 
  608. computer treats N as if it were a whole number (3); so in that 
  609. example, if N is 3.1, the computer will go to line 20.
  610.                              Christmas Remember the Christmas 
  611. carol called ``The Twelve Days of Christmas''? The first three 
  612. verses go like this:
  613. ON THE FIRST DAY OF CHRISTMAS, MY TRUE LOVE SENT TO ME
  614. A PARTRIDGE IN A PEAR TREE.
  615.  
  616. ON THE SECOND DAY OF CHRISTMAS, MY TRUE LOVE SENT TO ME
  617. TWO TURTLE DOVES...AND
  618. A PARTRIDGE IN A PEAR TREE.
  619.  
  620. ON THE THIRD DAY OF CHRISTMAS, MY TRUE LOVE SENT TO ME
  621. THREE FRENCH HENS,
  622. TWO TURTLE DOVES...AND
  623. A PARTRIDGE IN A PEAR TREE.
  624.                              This program prints the final verse:
  625. 10 PRINT "ON THE TWELFTH DAY OF CHRISTMAS, MY TRUE LOVE SENT TO 
  626. ME"
  627. 20 PRINT "TWELVE DRUMMERS DRUMMING,"
  628. 30 PRINT "ELEVEN PIPERS PIPING,"
  629. 40 PRINT "TEN LORDS A-LEAPING,"
  630. 50 PRINT "NINE LADIES DANCING,"
  631. 60 PRINT "EIGHT MAIDS A-MILKING,"
  632. 70 PRINT "SEVEN SWANS A-SWIMMING,"
  633. 80 PRINT "SIX GEESE A-LAYING,"
  634. 90 PRINT "FIVE GO---OLD RINGS,"
  635. 100 PRINT "FOUR CALLING BIRDS,"
  636. 110 PRINT "THREE FRENCH HENS,"
  637. 120 PRINT "TWO TURTLE DOVES...AND"
  638. 130 PRINT "A PARTRIDGE IN A PEAR TREE."
  639.                              This program prints all twelve 
  640. verses:
  641. 1 DATA FIRST,SECOND,THIRD,FOURTH,FIFTH,SIXTH,SEVENTH,EIGHTH,NINTH
  642. 2 DATA TENTH,ELEVENTH,TWELFTH                                    
  643. 3 FOR V = 1 TO 12                                                
  644. 4      READ W$                                                   
  645. 10     PRINT "ON THE ";W$;" DAY OF CHRISTMAS, MY TRUE LOVE SENT 
  646. TO ME"
  647. 11     ON V GO TO 130,120,110,100,90,80,70,60,50,40,30,20
  648. 20     PRINT "TWELVE DRUMMERS DRUMMING,"
  649. 30     PRINT "ELEVEN PIPERS PIPING,"
  650. 40     PRINT "TEN LORDS A-LEAPING,"
  651. 50     PRINT "NINE LADIES DANCING,"
  652. 60     PRINT "EIGHT MAIDS A-MILKING,"
  653. 70     PRINT "SEVEN SWANS A-SWIMMING,"
  654. 80     PRINT "SIX GEESE A-LAYING,"
  655. 90     PRINT "FIVE GO---OLD RINGS,"
  656. 100    PRINT "FOUR CALLING BIRDS,"
  657. 110    PRINT "THREE FRENCH HENS,"
  658. 120    PRINT "TWO TURTLE DOVES...AND"
  659. 130    PRINT "A PARTRIDGE IN A PEAR TREE."
  660. 140    PRINT
  661. 150 NEXT    
  662.                              Lines 1 and 2 teach the computer how 
  663. to spell the words ``FIRST'', ``SECOND'', ``THIRD'', etc. Line 3 
  664. makes the computer do lines 4 through 140, twelve times (once for 
  665. each verse). Lines 4 and 10 look at the data and print the top 
  666. line of the verse. Line 11 says:
  667. If V is 1, go to line 130 (so the computer prints A PARTRIDGE IN 
  668. A PEAR TREE).
  669.  
  670. If V is 2, go to line 120 (so the computer prints TWO TURTLE 
  671. DOVES...AND A PARTRIDGE IN A PEAR TREE).
  672.  
  673. If V is 3, go to line 110 (so the computer prints THREE FRENCH 
  674. HENS, TWO TURTLE DOVES...AND A PARTRIDGE IN A PEAR TREE).
  675.  
  676. And so on, for each verse.
  677.  
  678.                                             ON . . . GOSUB
  679.                              You can say:
  680. 50 ON N GOSUB 80,100,20,350
  681.                              That means:
  682. If N is 1, GOSUB 80.
  683. If N is 2, GOSUB 100.
  684. If N is 3, GOSUB 20.
  685. If N is 4, GOSUB 350.
  686.  
  687.      ON ERROR GO TO
  688.   If the computer finds an error while running your program, the 
  689. computer wants to gripe. But instead of letting the computer 
  690. gripe, you can force the computer to do something different.
  691.   For example, you can tell the computer, ``If you find an error 
  692. in lines 50-70, don't gripe; instead of griping, do the special 
  693. routine that begins at line 1000.'' To tell the computer that, 
  694. insert these lines:
  695. 49 ON ERROR GO TO 1000
  696. 71 ON ERROR GO TO 0
  697. Lines 49 and 71 tell the computer, ``If you find an error between 
  698. lines 49 and 71, go to the special routine that begins at line 
  699. 1000.''
  700.   Invent your own special routine, so that it begins at line 1000 
  701. and continues onto lines 1010, 1020, etc.
  702.   The bottom line of your special routine should tell the 
  703. computer where to go afterwards.
  704.   For example, suppose that after the routine is done, you want 
  705. the computer to go back to line 30 (to try again). Just put this 
  706. line at the bottom of the routine:
  707. 1090 RESUME 30
  708. That way, if an error occurs between lines 49 and 71, the 
  709. computer will do the routine in lines 1000-1080, then come to 
  710. line 1090, which sends the computer back to line 30.
  711.   The special routine (in lines 1000-1090) is called an 
  712. error-handling routine or an error trap. Its bottom line says 
  713. RESUME. (By contrast, the bottom line of an ordinary subroutine 
  714. says RETURN instead.)
  715.   To separate the main routine (lines 10-999) from the 
  716. error-handing routine (lines 1000-1090), the bottom line of the 
  717. main routine should say END, like this:
  718. 999 END
  719.   Instead of saying RESUME 30, which sends the computer back to 
  720. line 30, you can send the computer back to any other line you 
  721. wish. For example, you can say RESUME 80.
  722.   If you don't put any number after RESUME, the computer will go 
  723. back to the line that contained the error.
  724.   If you say RESUME NEXT, the computer will go to the line 
  725. underneath the line that contained the error. To make RESUME NEXT 
  726. work properly, the line that contained the error should consists 
  727. of just one statement (instead of statements separated by 
  728. colons).
  729.  
  730.                   MEMORY CELLS
  731.                              The computer's main memory consists 
  732. of many memory cells. Each cell holds an integer from 0 to 255. 
  733. For example, cell #7512 might hold the integer 17.
  734.  
  735.                                                  PEEK
  736.                              To find out what number's in cell 
  737. #7512, say:
  738. PRINT PEEK(7512)
  739. That makes the computer peek at cell #7512, find the number in 
  740. that cell, and print that number on your screen. The number it 
  741. prints will be an integer from 0 to 255. For example, it might be 
  742. 17.
  743.  
  744.                                                  POKE
  745.                              The memory contains two kinds of 
  746. cells. One kind, called ROM, contains information permanently. 
  747. The other kind, called RAM, contains information temporarily, and 
  748. you can change that information. When you turn the computer on, 
  749. each RAM cell contains a 0, but you can change the zeros to other 
  750. numbers.
  751.                              If you say ___ 
  752. POKE 7512,14
  753. the computer will try to put the number 14 into cell #7512. If 
  754. cell #7512 is in the RAM, the computer will succeed. If cell 
  755. #7512 is in the ROM, the computer will give up, since the 
  756. information in ROM cells is permanent and can't be changed.
  757.                              To find out whether the computer 
  758. successfully put the number 14 into cell #7512, say:
  759. PRINT PEEK(7512)
  760. If the computer prints 14, the computer successfully poked. If 
  761. the computer prints a different number instead, the computer's 
  762. POKE was unsuccessful, which means cell #7512 is in the ROM and 
  763. therefore can't be changed.
  764.                              When you turn the computer on, the 
  765. ROM cells contain their permanent information, but the RAM cells 
  766. are ``blank''; each RAM cell contains 0. To change the numbers in 
  767. the RAM cells, say POKE.
  768.  
  769.                                             How RAM is used
  770.                              Whenever you type a new program or 
  771. command or data, the computer temporarily stores what you typed 
  772. in the RAM.
  773.                              For example, if you type the word 
  774. CAB (because you're writing a program about taxicabs), the 
  775. computer temporarily puts the ASCII code numbers for C, A, and B 
  776. into RAM cells. Since C's ASCII code number is 67, A's is 65, and 
  777. B's is 66, the computer puts the numbers 67, 65, and 66 into 
  778. three RAM cells.
  779.                              As you type more programs, commands, 
  780. and data, the computer puts their ASCII code numbers into RAM 
  781. cells. Into other RAM cells, the computer puts notes about what 
  782. you and the computer are doing.
  783.                              If you say to POKE a number into a 
  784. RAM cell, beware: the computer might already be using that cell 
  785. to hold an important note. The number you POKE into the cell will 
  786. replace the computer's note. The computer will forget the note, 
  787. get confused, and go crazy. If the RAM cell contained a note 
  788. about your program, the computer might wreck your program; if the 
  789. RAM cell contained a note about the disk drive, the computer 
  790. might go so crazy that it will turn the drive on and wreck all 
  791. the information on the disk!
  792.                              So before you say POKE, find out 
  793. which RAM cells the computer uses for which purposes ___ and as 
  794. an extra precaution, remove your disk from the drive. Don't 
  795. reinsert the disk until you've turned off the computer and 
  796. started fresh again.
  797.  
  798.         SEQUENTIAL DATA FILES
  799.   Here's a simple program:
  800. 10 PRINT "EAT"
  801. 20 PRINT 2+2
  802. 30 PRINT "EGGS"
  803.   It makes the computer print this message onto your screen:
  804. EAT
  805.  4
  806. EGGS
  807.   Instead of printing that message onto your screen, let's make 
  808. the computer print the message onto your disk. Here's how. . . . 
  809.  
  810.            OPEN FOR OUTPUT
  811.   This program prints the message onto your disk, instead of onto 
  812. your screen:
  813. 5 OPEN "SUE" FOR OUTPUT AS 1
  814. 10 PRINT#1, "EAT"
  815. 20 PRINT#1, 2+2
  816. 30 PRINT#1, "EGGS"
  817. 40 CLOSE
  818.   Lines 10-30 make the computer print the message onto your disk, 
  819. instead of onto your screen. Each line says PRINT#1, which means: 
  820. print onto the disk.
  821.   Line 5 is an introductory line that tells the computer where on 
  822. the disk to print the message. Line 5 says: find a blank place on 
  823. the disk, call it ``SUE'', and make ``SUE'' be file#1. So 
  824. PRINT#1, will mean: print onto file#1, which is SUE.
  825.   Any program that says OPEN should also say CLOSE, so line 40 
  826. says CLOSE. The CLOSE line makes the computer put some 
  827. ``finishing touches'' on SUE, so that SUE becomes a perfect, 
  828. finished file.
  829.   When you RUN that program, the computer will automatically put 
  830. onto the disk a file called ``SUE'' that contains this message:
  831. EAT
  832.  4
  833. EGGS
  834.   After running the program, if you want to see the names of all 
  835. the files on the disk, type FILES (or DIRECTORY or CATALOG or 
  836. whatever other word your computer uses). The computer will print 
  837. the names of all the files ___ and one of the names it prints 
  838. will be SUE.
  839.                                                   OPEN FOR INPUT
  840.                                          To see the message 
  841. that's in SUE, run this program, which inputs from SUE and prints 
  842. onto your screen:
  843. 5 OPEN "SUE" FOR INPUT AS 1
  844.  
  845. 10 INPUT#1, A$
  846. 11 PRINT A$
  847.  
  848. 20 INPUT#1, B
  849. 21 PRINT B
  850.  
  851. 30 INPUT#1, C$
  852. 31 PRINT C$
  853.  
  854. 40 CLOSE
  855.                                          Line 5 prepares the 
  856. computer to input from SUE.
  857.                                          Line 10 inputs A$ from 
  858. SUE, so A$ is EAT. Line 11 prints EAT onto your screen.
  859.                                          Line 20 inputs B from 
  860. SUE, so B is 4. Line 21 prints 4 onto your screen.
  861.                                          Line 30 inputs C$ from 
  862. SUE, so C$ is EGGS. Line 31 prints EGGS onto your screen. So 
  863. altogether, on your screen you'll see:
  864. EAT
  865.  4
  866. EGGS
  867.                                          Line 40 tells the 
  868. computer that you're done using SUE for a while (until you say 
  869. OPEN again).
  870.  
  871.                                                   OPEN FOR APPEND
  872.                                          After you've put SUE 
  873. onto the disk, so that SUE consists of EAT and 4 and EGGS, try 
  874. running this program:
  875. 10 OPEN "SUE" FOR APPEND AS 1
  876. 20 PRINT#1, "GOOD MORNING!"
  877. 30 CLOSE
  878.                                          In line 10, the word 
  879. APPEND tells the computer to keep adding onto SUE. So when the 
  880. computer comes to line 20, it adds ``GOOD MORNING'' onto SUE, and 
  881. SUE becomes this:
  882. EAT
  883.  4
  884. EGGS
  885. GOOD MORNING!
  886.  
  887.                                                       Erasing
  888.                                          For your next 
  889. experiment, try running this program:
  890. 10 OPEN "SUE" FOR OUTPUT AS 1
  891. 20 PRINT#1, "PICKLES ARE PLEASANT"
  892. 30 CLOSE
  893.                                          Since line 10 does not 
  894. say APPEND, the computer will not keep adding onto SUE. Instead, 
  895. the computer erases everything that's been in SUE. So when the 
  896. computer finishes processing line 10, SUE's become blank.
  897.                                          Line 20 puts ``PICKLES 
  898. ARE PLEASANT'' into SUE. So at the end of the program, SUE 
  899. includes ``PICKLES ARE PLEASANT''; but SUE does not include EAT 
  900. and 4 and EGGS and ``GOOD MORNING'', which have all been erased.
  901.                       Loops
  902.   This program lets you put the names of all your friends onto 
  903. the disk:
  904. 10 OPEN "FRIENDS" FOR OUTPUT AS 1
  905. 20 PRINT "PLEASE TYPE A FRIEND'S NAME (OR THE WORD 'END')"
  906. 30 INPUT F$: IF F$="END" THEN CLOSE: END
  907. 40 PRINT#1, F$
  908. 50 GO TO 20
  909.   Line 10 makes the computer find a blank space on the disk and 
  910. call it FRIENDS. Line 20 makes the computer print:
  911. PLEASE TYPE A FRIEND'S NAME (OR THE WORD 'END')
  912.   Line 30 prints a question mark and waits for you to type 
  913. something; whatever you type will be called F$. For example, if 
  914. you type JOAN WILLIAMS, then F$ will be JOAN WILLIAMS, and line 
  915. 40 prints the name JOAN WILLIAMS onto the disk.
  916.   Line 50 creates a loop, so that you can type as many names as 
  917. you wish. (Remember to press the ENTER key after each name.)
  918.   When you've finished typing the names of all your friends, type 
  919. the word END. Then the last part of line 30 will make the 
  920. computer CLOSE the file and END the program.
  921.   This program makes the computer look at the FRIENDS file and 
  922. copy all its names to your screen:
  923. 10 OPEN "FRIENDS" FOR INPUT AS 1
  924. 20 IF EOF(1) THEN PRINT "THOSE ARE ALL THE FRIENDS": CLOSE: END
  925. 30 INPUT#1, F$
  926. 40 PRINT F$
  927. 50 GO TO 20
  928.   Line 10 prepares the computer to input from the FRIENDS file. 
  929. Line 20 is special; I'll explain it later. Line 30 makes the 
  930. computer input a string from the file and call the string F$; so 
  931. F$ becomes the name of one of your friends. Line 40 prints that 
  932. friend's name onto your screen. Line 50 creates a loop, so that 
  933. the names of all your friends are printed on the screen.
  934.   Eventually, the computer will reach the end of the file, and 
  935. there won't be any more names to input from the file. Line 20 
  936. says: if the computer reaches the End Of the File and can't input 
  937. any more names from it, the computer should print on your screen 
  938. ``THOSE ARE ALL THE FRIENDS'', then CLOSE the file and END the 
  939. program.
  940.  
  941.                        LOF
  942.   In the middle of your program, if you say PRINT LOF(1), the 
  943. computer will tell you the Length Of the File: it will tell you 
  944. how many bytes are in the file.
  945.  
  946.                  Multiple files
  947.   If you want the computer to handle two files simultaneously, 
  948. use two OPEN statements. At the end of the first OPEN statement, 
  949. say ``AS 1''; at the end of the second OPEN statement, say ``AS 
  950. 2''.
  951.   For the second file, say PRINT#2 instead of PRINT#1, say 
  952. INPUT#2 instead of INPUT#1, say EOF(2) instead of EOF(1), and say 
  953. LOF(2) instead of LOF(1).
  954.  
  955.                   How to CLOSE
  956.   The CLOSE statement closes all files. To be more specific, you 
  957. can say CLOSE 1 (which closes just the first file) or CLOSE 2 
  958. (which closes just the second).
  959.   Whenever you're done using a file, CLOSE it immediately. When 
  960. you say CLOSE, the computer puts finishing touches on the file 
  961. that protect the file against damage.
  962.   Suppose that halfway through your program, you finish using 
  963. file 2 but want to continue using file 1. Say CLOSE 2 there, and 
  964. delay saying CLOSE 1 until later.
  965.  
  966.  
  967.             RANDOM ACCESS
  968.   On a disk, you can store two kinds of data files. The simple 
  969. kind is called a sequential-access data file; the complicated 
  970. kind is called a random-access (or relative-access or 
  971. direct-access) data file. You've already learned how to create 
  972. and retrieve a sequential-access file. Now let's look at 
  973. random-access.
  974.   Though more complicated than sequential-access data files, 
  975. random-access data files have an advantage: they let you skip 
  976. around. In a sequential-access data file, you must look at the 
  977. first item of data, then the second, then the third, etc. In a 
  978. random-access data file, you can look at the seventh item of 
  979. data, then skip directly to the tenth, then hop back to the 
  980. third, then skip directly to the sixth, etc.
  981.   Each item is called a record. The number of characters (bytes) 
  982. in the record is called the record's length. For example, if a 
  983. record contains 30 characters, the record's length is 30. In a 
  984. random-access file, all the records must have the same length as 
  985. each other.
  986.  
  987.                  PUT
  988.   Let's create a random-access file called JIM. Let's make JIM's 
  989. record length be 20, so that each record will contain 20 
  990. characters. Here's how:
  991. 5 OPEN "JIM" AS 1 LEN=20: FIELD 1, 20 AS X$
  992.   Let's make JIM's 7th record be ``LOVE MAKES ME GIGGLE'' (which 
  993. contains 20 characters):
  994. 10 LSET X$="LOVE MAKES ME GIGGLE": PUT 1,7
  995.   Let's make JIM's 9th record be ``PLEASE HOLD MY HAND'':
  996. 20 LSET X$="PLEASE HOLD MY HAND": PUT 1,9
  997. Since JIM's record length is supposed to be 20 characters but 
  998. ``PLEASE HOLD MY HAND'' contains just 19 characters, the computer 
  999. will automatically add a blank to the end of ``PLEASE HOLD MY 
  1000. HAND''.
  1001.   Let's make JIM's 4th record be ``I LOVE LUCY'':
  1002. 30 LSET X$="I LOVE LUCY": PUT 1,4
  1003. The computer will automatically add blanks to the end of ``I LOVE 
  1004. LUCY''.
  1005.   To finish the program, say:
  1006. 40 CLOSE
  1007.  
  1008.                  GET
  1009.   This program makes the computer tell you JIM's 7th item:
  1010. 5 OPEN "JIM" AS 1 LEN=20: FIELD 1, 20 AS X$
  1011. 10 GET 1,7: PRINT X$
  1012. 20 CLOSE
  1013.  
  1014.          Multi-field records
  1015.   If you want each record to be a pair of strings, begin like 
  1016. this:
  1017. 5 OPEN "JIM" AS 1 LEN=34: FIELD 1, 30 AS X$, 4 AS Y$
  1018. 10 LSET X$="LOVE MAKES ME GIGGLE": LSET Y$="WOW": PUT 1,7
  1019. etc.
  1020.  
  1021.                                          Line 5 makes each record 
  1022. consist of two fields. The first field is a 30-character string 
  1023. called X$; the second field is a 4-character string called Y$.
  1024.                                          Line 10 says to PUT into 
  1025. the 7th record this pair of strings: ``LOVE MAKES ME GIGGLE'' and 
  1026. ``WOW''.
  1027.  
  1028.                                                         LOC
  1029.                                          While using a 
  1030. random-access file, if you say PRINT LOC(1), the computer tells 
  1031. you which record it just dealt with: it tells you the record 
  1032. LOCation. For example, if you say ``PUT 1,7'' or ``GET 1,7'' and 
  1033. then say PRINT LOC(1), the computer prints the number 7.
  1034.                                          If you say ``PUT 1'' 
  1035. instead of ``PUT 1,7'', the computer will assume you mean ``PUT 
  1036. 1,LOC(1)+1''. If you say ``GET 1'', the computer will assume you 
  1037. mean ``GET 1,LOC(1)+1''.
  1038.  
  1039.                                                   End of the file
  1040.                                          The EOF function doesn't 
  1041. work well for random-access files. To deal with the end of the 
  1042. file, use the following trick instead.
  1043.                                          Suppose you say LEN=34, 
  1044. so that 34 bytes make a record. Since the number of bytes in the 
  1045. entire file is LOF(1), and 34 bytes make a record, the number of 
  1046. records in the file is LOF(1)/34. These lines print all the 
  1047. records:
  1048. 100 FOR I = 1 TO LOF(1)/34
  1049. 110     GET 1: PRINT X$;Y$
  1050. 120 NEXT
  1051.  
  1052.                                           Restrictions on FIELD variables
  1053.                                          If you put a variable 
  1054. (such as X$) into a FIELD statement, you cannot put it into an 
  1055. ordinary ``='' statement: instead of saying X$=``LOVE MAKES ME 
  1056. GIGGLE'', you must say LSET X$=``LOVE MAKES ME GIGGLE''. You 
  1057. cannot put the variable into an INPUT statement: instead of 
  1058. saying INPUT X$, you must say INPUT A$ and then LSET X$=A$.
  1059.  
  1060.                                                   Numerical data
  1061.                                          On most computers, a 
  1062. random-access file must contain strings, not numbers. If you want 
  1063. to store numbers, you must turn them into strings.
  1064.                                          To turn an integer into 
  1065. a string, use the function MKI$ (MaKe from Integer). It turns the 
  1066. integer into a 2-byte string, even if the integer is long. For 
  1067. example, to turn the integer 17999 into a 2-byte string called 
  1068. X$, say FIELD 1, 2 AS X$ and say LSET X$=MKI$(17999).
  1069.                                          The function MKS$ (MaKe 
  1070. from Single-precision real) turns a real number into a 4-byte 
  1071. string. The function MKD$ (MaKe from Double-precision) turns a 
  1072. double-precision number into an 8-byte string.
  1073.                                          Suppose you turn a 
  1074. number into a string (by using MKI$, MKS$, or MKD$), and PUT the 
  1075. string into a file, and later GET the string back from the file. 
  1076. You'll want to convert the string back to a number. Use the 
  1077. function CVI (ConVert to Integer) or CVS (ConVert to 
  1078. Single-precision) or CVD (ConVert to Double-precision). For 
  1079. example, if X$ is a 2-byte string that stands for an integer, you 
  1080. can make the computer print the integer by saying PRINT CVI(X$).
  1081.  
  1082.                 CREATE A DATABASE
  1083.   The following program creates a database, in which you can 
  1084. store information about your friends & enemies, your business & 
  1085. bills, birthdays & appointments, desires & dreads, and whatever 
  1086. else bothers you. After storing the information in the database, 
  1087. you can peek at the information, change it, expand on it, delete 
  1088. it, or do whatever else strikes your fancy.
  1089.  
  1090.              Chronological database
  1091.   The program consists of a main routine and 7 subroutines:
  1092. The main routine
  1093. All variables will be integers.10 DEFINT A-Z
  1094. Allow 100 topics & their data.20 DIM T$(100),D$(100)
  1095. Number of topics starts at 0.30 N=0
  1096. Ask the human for a topic.40 PRINT: PRINT "WHAT TOPIC INTERESTS 
  1097. YOU?": PRINT "(IF YOU'RE NOT SURE, TYPE A QUESTION MARK)": L
  1098.                 INE INPUT T$
  1099. Do what the human wishes.50 IF T$="?" THEN GOSUB 1000 ELSE GOSUB 
  1100. 2000
  1101. Go to another topic.60 GO TO 40
  1102.  
  1103. Subroutine 1000: tell the human what topics are in the database
  1104. If database is empty, say so.1000 IF N=0 THEN PRINT "I DON'T KNOW 
  1105. ANY TOPICS YET.": PRINT "MY MIND IS STILL BLANK.": PRINT "PLE ASE 
  1106. TEACH ME A NEW TOPIC.": RETURN
  1107. If not empty, list the topics.1010 PRINT "I KNOW ABOUT THESE 
  1108. TOPICS:": FOR I = 1 TO N: PRINT T$(I),: NEXT I: PRINT: PRINT 
  1109. "PICK 
  1110.                 ONE OF THOSE TOPICS, OR TEACH ME A NEW ONE."
  1111.                 1020 RETURN
  1112.  
  1113. Subroutine 2000: search through the database, to find the topic 
  1114. T$
  1115. Start searching.2000 FOR I = 1 TO N
  1116. If the topic is found, do 3000.2010    IF T$=T$(I) THEN GOSUB 
  1117. 3000: RETURN
  1118. If not found yet, try again.2020 NEXT
  1119. If never found, do 4000.2030 GOSUB 4000
  1120.                 2040 RETURN
  1121.  
  1122. Subroutine 3000: the topic's in the database
  1123. Tell the human about the topic.3000 PRINT "HERE'S WHAT I KNOW 
  1124. ABOUT "T$":": PRINT D$(I)
  1125. Ask whether to change the info.3010 INPUT "DO YOU WANT TO CHANGE 
  1126. THAT INFORMATION";A$
  1127. If the human says yes, do 5000.3020 IF A$="YES" OR A$="Y" THEN 
  1128. GOSUB 5000
  1129.                 3030 RETURN
  1130.  
  1131. Subroutine 4000: the topic's not in the database
  1132. Say topic is not in database.4000 PRINT "I DON'T KNOW ANYTHING 
  1133. ABOUT "T$"."
  1134. Request info about the topic.4010 PRINT: PRINT "TELL ME ABOUT 
  1135. "T$: PRINT "(IF YOU DON'T WANT TO TELL ME, TYPE THE LETTER X)": L
  1136.                 INE INPUT D$
  1137. If human wants, insert topic.4020 IF D$<>"X" THEN GOSUB 6000
  1138.                 4030 RETURN
  1139.  
  1140. Subroutine 5000: change the info
  1141. Agree to change the info.5000 PRINT "OKAY. I'VE ERASED THAT 
  1142. INFORMATION ABOUT "T$"."
  1143. Request new info.5010 PRINT: PRINT "TYPE WHAT YOU WANT ME TO KNOW 
  1144. ABOUT "T$: PRINT "(IF YOU WANT ME TO FORGET "T$",
  1145.                  TYPE THE LETTER X)": INPUT D$
  1146. Change the info as requested.5020 IF D$="X" THEN GOSUB 7000 ELSE 
  1147. D$(I)=D$
  1148.                 5030 RETURN
  1149.  
  1150. Subroutine 6000: insert the topic into the database
  1151. Increase the number of topics.6000 N=N+1
  1152. Append new topic & its data.6010 T$(N)=T$: D$(N)=D$
  1153.                 6020 RETURN
  1154.  
  1155. Subroutine 7000: delete the topic from the database
  1156. Replace that topic by topic #N.7000 T$(I)=T$(N): D$(I)=D$(N)
  1157. Decrease the number of topics.7010 N=N-1
  1158.                 7020 RETURN
  1159.   If your computer doesn't understand DEFINT, omit line 10. If 
  1160. your computer doesn't understand LINE INPUT, omit the word LINE 
  1161. from lines 40 and 4010.
  1162.   The program stores the topics in chronological order: if you 
  1163. begin by feeding it information about SUE and then information 
  1164. about CAROL, it will let T$(1) be SUE and let T$(2) be CAROL.
  1165.  
  1166.               Alphabetical database
  1167.   Instead of chronological order, you might prefer alphabetical 
  1168. order.
  1169.   For example, suppose you feed the computer information about 
  1170. SUE then CAROL then ZELDA then ALICE then JANE. Here's what the 
  1171. computer's memory would look like, in each kind of order:
  1172. Chronological orderAlphabetical order
  1173. SUE           ALICE
  1174. CAROL         CAROL
  1175. ZELDA         JANE
  1176. ALICE         SUE
  1177. JANE          ZELDA
  1178.   Which is better: chronological order or alphabetical order?
  1179.   Chronological order lets you quickly add a new name (just add 
  1180. it at the end of the list), but finding a name in the list is 
  1181. slow (since the list looks disorganized). Alphabetical order lets 
  1182. you find a name faster (since the list is alphabetized), but 
  1183. adding a new name to the alphabetized list is slow (since the 
  1184. only way to insert the new name is to make room for it by shoving 
  1185. other names out of the way).
  1186.   So which is better?
  1187. Chronological order is the simplest to program and the fastest 
  1188. for INSERTING.
  1189. Alphabetical order is the fastest for FINDING information.
  1190.   If you want to store the names in alphabetical order instead of 
  1191. chronological order, just change subroutines 2000, 6000, and 7000 
  1192. to these:
  1193. Subroutine 2000A: search through the database, to find the topic 
  1194. T$
  1195. Create L and H. 2000 L=0: H=N+1
  1196. I is the average of L and H.2010 I=INT((L+H+1)/2)
  1197. If I=H, do 4000.2020 IF I=H THEN GOSUB 4000: RETURN
  1198. If the topic is found, do 3000.2030 IF T$=T$(I) THEN GOSUB 3000: 
  1199. RETURN
  1200. Not found yet. Change H or L2040 IF T$<T$(I) THEN H=I ELSE L=I
  1201. and try again to find topic!2050 GO TO 2010
  1202.  
  1203. Subroutine 6000A: insert the topic into the database
  1204. Increase the number of topics.6000 N=N+1
  1205. Move other topics out of way.6010 FOR J = N TO I+1 STEP -1: 
  1206. T$(J)=T$(J-1): D$(J)=D$(
  1207.                 J-1): NEXT
  1208. Insert new topic & its data.6020 T$(I)=T$: D$(I)=D$
  1209.                 6030 RETURN
  1210.  
  1211. Subroutine 7000A: delete the topic from the database
  1212. Decrease the number of topics.7000 N=N-1
  1213. Close gap from deleted topic.7010 FOR J = 1 TO N: T$(J)=T$(J+1): 
  1214. D$(J)=D$(J+1): NEXT
  1215.                 7020 RETURN
  1216.   Subroutine 2000A runs faster than chronological subroutine 
  1217. 2000, because searching through an alphabetical list is faster 
  1218. than searching through a chronological list. (To search through 
  1219. the alphabetical list super-quickly, subroutine 2000A uses a 
  1220. trick called binary search.)
  1221.   Unfortunately, subroutines 6000A (which inserts) and 7000A 
  1222. (which deletes) run slower than chronological subroutines 6000 
  1223. and 7000. To get the high speed of subroutine 2000A, you must 
  1224. accept the slowness of subroutines 6000A and 7000A.
  1225.   You've seen that chronological order is fast for inserting and 
  1226. deleting but slow for searching, whereas alphabetical order is 
  1227. exactly the opposite: it's fast for searching but slow for 
  1228. inserting or deleting.
  1229.  
  1230.             Tree-structured database
  1231.   Instead of using chronological order or alphabetical order, 
  1232. advanced programmers use a tree. Like chronological order, a tree 
  1233. lets you insert and delete quickly. Like alphabetical order, a 
  1234. tree lets you search quickly also.
  1235.   Poets say, ``only God can make a tree.'' Does that mean 
  1236. advanced programmers are God?
  1237.   To learn how to make a tree, begin by sketching a picture of a 
  1238. tree on paper. Since N is the alphabet's middle letter, begin by 
  1239. writing the letter N, and put two arrows underneath it:
  1240.                    N
  1241.  
  1242.  
  1243. The left arrow is called the before-arrow; it will point to the 
  1244. names that come alphabetically before N. The right arrow is 
  1245. called the after-arrow; it will point to the names that come 
  1246. alphabetically after N.
  1247.                                                      For example, 
  1248. suppose your first topic is SUE. Since SUE comes alphabetically 
  1249. after N, put SUE at the tip of N's after-arrow:
  1250.                    N
  1251.  
  1252.                             SUE
  1253.  
  1254.  
  1255.                                                      Suppose your 
  1256. next topic is CAROL. Since CAROL comes alphabetically before N, 
  1257. put CAROL at the tip of N's before-arrow:
  1258.                    N
  1259.  
  1260.        CAROL                SUE
  1261.  
  1262.  
  1263.                                                      Suppose your 
  1264. next topic is ZELDA. Since ZELDA comes after N, we'd like to put 
  1265. ZELDA at the tip of N's after-arrow; but SUE's already stolen 
  1266. that position. So compare ZELDA against SUE. Since ZELDA comes 
  1267. after SUE, put ZELDA at the tip of SUE's after-arrow:
  1268.                    N
  1269.  
  1270.        CAROL                SUE
  1271.  
  1272.                                 ZELDA
  1273.  
  1274.  
  1275.                                                      Suppose your 
  1276. next topic is ALICE. Since ALICE comes before N, look at the tip 
  1277. of N's before-arrow. Since CAROL's stolen that position, compare 
  1278. ALICE against CAROL; since ALICE comes before CAROL, put ALICE at 
  1279. the tip of CAROL's before-arrow:
  1280.                    N
  1281.  
  1282.        CAROL                SUE
  1283.  
  1284.   ALICE                         ZELDA
  1285.  
  1286.  
  1287.                                                      Suppose your 
  1288. next topic is JANE. Since JANE comes before N, look at the tip of 
  1289. N's before-arrow. Since CAROL's stolen that position, compare 
  1290. JANE against CAROL; since JANE comes after CAROL, put JANE at the 
  1291. tip of CAROL's after-arrow:
  1292.                    N
  1293.  
  1294.        CAROL                SUE
  1295.  
  1296.   ALICE     JANE                ZELDA
  1297.  
  1298.  
  1299.  
  1300.   If the next few topics are FRED, then LOU, then RON, then BOB, 
  1301. the tree looks like this:
  1302.                    N
  1303.  
  1304.        CAROL                SUE
  1305.  
  1306.   ALICE     JANE       RON      ZELDA
  1307.  
  1308.      BOB  FRED LOU
  1309.  
  1310.  
  1311. Look at the arrows that point down from N. N's before-arrow 
  1312. points to the group of names that come alphabetically before N 
  1313. (such as CAROL, ALICE, JANE, BOB, FRED, and LOU); N's after-arrow 
  1314. points to the group of names that come alphabetically after N 
  1315. (such as SUE, RON, and ZELDA). Similarly, CAROL's before-arrow 
  1316. points to the group of names that come alphabetically before 
  1317. CAROL (such as ALICE and BOB); CAROL's after-arrow points to the 
  1318. group of names that come alphabetically after CAROL (such as 
  1319. JANE, FRED, and LOU).
  1320.   Programmers treat the tree as if it were a ``family tree''. 
  1321. CAROL is called the parent of ALICE and JANE, who are therefore 
  1322. called CAROL's children. CAROL is called the ancestor of ALICE, 
  1323. JANE, BOB, FRED, and LOU, who are therefore called CAROL's 
  1324. descendants. The arrows are called pointers.
  1325.   To make the tree more useful, begin with ``N !'' instead of 
  1326. ``N'' (so you can choose ``N'' as a topic later), and number the 
  1327. topics in the order they appeared: since SUE was the first topic, 
  1328. put ``1'' in front of SUE; since CAROL was the second topic, put 
  1329. ``2'' in front of CAROL; since ZELDA was the third topic, put 
  1330. ``3'' in front of ZELDA, like this:
  1331.                   N !
  1332.  
  1333.       2CAROL               1SUE
  1334.  
  1335.  4ALICE    5JANE      8RON     3ZELDA
  1336.  
  1337.     9BOB 6FRED7LOU
  1338.  
  1339.  
  1340.   To describe the tree to the computer, store this table in the 
  1341. computer's memory:
  1342. Topic                            Where the before-arrow 
  1343. pointsWhere the after-arrow points
  1344. 0 N !                                    2                     1
  1345. 1 SUE                                    8                     3
  1346. 2 CAROL                                  4                     5
  1347. 3 ZELDA                                  0                     0
  1348. 4 ALICE                                  0                     9
  1349. 5 JANE                                   6                     7
  1350. 6 FRED                                   0                     0
  1351. 7 LOU                                    0                     0
  1352. 8 RON                                    0                     0
  1353. 9 BOB                                    0                     0
  1354. That table represents the tree and is called the tree's 
  1355. representation.
  1356.                              The table's left column is in 
  1357. chronological order, but the other columns give information about 
  1358. alphabetizing. So a tree combines chronological order with 
  1359. alphabetical order: it combines the advantages of both. Adding a 
  1360. new topic to the tree is quick and easy (as in chronological 
  1361. order): just add the name to the bottom of the list, and adjust a 
  1362. few arrows. Using the tree to search for a topic is quick and 
  1363. easy (as in alphabetical order): just follow the arrows.
  1364.                              Like the alphabetical program, the 
  1365. program that creates and manipulates the tree uses the same 
  1366. subroutines 1000, 3000, 4000, and 5000 as the chronological 
  1367. program. To create the tree, change the main routine and 
  1368. subroutines 2000, 6000, and 7000. You must also add a new 
  1369. subroutine (8000). Here are those new routines:
  1370. The main routine T
  1371. All variables will be integers.            10 DEFINT A-Z
  1372. Allow 100 topics, data, etc.               20 DIM 
  1373. T$(100),D$(100),P(100,2)
  1374. Topic #0 is "N !".                         30 T$(0)="N !": 
  1375. P(0,1)=0: P(0,2)=0
  1376. The number of real topics is 0.            40 N=0
  1377. Ask the human for a topic.                 50 PRINT: PRINT "WHAT 
  1378. TOPIC INTERESTS YOU?": PRINT "(I
  1379.                                            F YOU'RE NOT SURE, 
  1380. TYPE A QUESTION MARK)": LINE INPUT  T$
  1381. Do what the human wishes.                  60 IF T$="?" THEN 
  1382. GOSUB 1000 ELSE GOSUB 2000
  1383. Go to another topic.                       70 GO TO 50
  1384.  
  1385. Subroutine 2000T: search through the database, to find the topic 
  1386. T$
  1387. Starting at 0, hunt for T$.                2000 I=0: GOSUB 8000
  1388. If absent, do 4000, else 3000.             2010 IF I=0 THEN GOSUB 
  1389. 4000 ELSE GOSUB 3000
  1390.                                            2020 RETURN
  1391.  
  1392. Subroutine 6000T: insert the topic into the database
  1393. Increase the number of topics.             6000 N=N+1
  1394. Append new topic & its data.               6010 T$(N)=T$: 
  1395. D$(N)=D$: P(N,1)=0: P(N,2)=0
  1396. Make topic I1 point to it.                 6020 P(I1,J)=N
  1397.                                            6030 RETURN
  1398.  
  1399. Subroutine 7000T: delete the topic from the database
  1400. Make topic I1 (which was pointing to the vanishing topic) point 
  1401. to the vanishing topic's left child instead.
  1402.                                            7000 P(I1,J)=P(I,1)
  1403. A is the number of the vanishing topic. B is the number of the 
  1404. vanishing topic's right child.
  1405.                                            7010 A=I: B=P(I,2)
  1406. If the vanishing topic has a right child, make something point to 
  1407. that child.
  1408.                                            7020 IF B>0 THEN 
  1409. T$=T$(B): I=I1: GOSUB 8000: P(I1,J)=B
  1410. If the vanishing topic isn't topic N, move topic N to the gap 
  1411. left by the vanishing topic.
  1412.                                            7030 IF A<N THEN 
  1413. T$=T$(N): I=0: GOSUB 8000: P(I1,J)=A:
  1414.                                             T$(A)=T$: 
  1415. D$(A)=D$(N): P(A,1)=P(N,1): P(A,2)=P(N,2)
  1416. Decrease the number of topics.             7040 N=N-1
  1417.                                            7050 RETURN
  1418.  
  1419. Subroutine 8000T
  1420. Starting at position I, hunt for T$ in the database. If T$ is 
  1421. missing from the database, make I be 0; otherwise make I be the 
  1422. position of T$. Find the topic that points to T$. Make I1 be that 
  1423. topic's position. If the arrow pointing to T$ points left, make J 
  1424. be 1; otherwise make J be 2. Here's how to do all that:
  1425.  
  1426. If the topic is found, return.             8000 IF T$=T$(I) THEN 
  1427. RETURN
  1428. Compute a tentative J.                     8010 IF T$<T$(I) THEN 
  1429. J=1 ELSE J=2
  1430. Compute a tentative I1 and I.              8020 I1=I: I=P(I,J)
  1431. If pointer is not 0, hunt again.           8030 IF I>0 THEN GO TO 
  1432. 8000
  1433.                                            8040 RETURN
  1434.  
  1435.                                           Disk-based database
  1436.                              All those database programs 
  1437. (chronological, alphabetical, and tree) put data into the RAM. 
  1438. When you turn off the power, the RAM forgets all the data!
  1439.   This superior program puts the database onto a disk instead of 
  1440. into RAM:
  1441. The main routine D
  1442. Prepare the variables.10 DEFINT A-Z: DIM PF$(2): Z$=MKI$(0)
  1443. Open the file and its fields.20 OPEN "INFO" AS 1 LEN=128: FIELD 
  1444. 1, 44 AS TF$, 80 AS DF$, 2 AS PF$(1), 2 AS PF$(2)
  1445. Topics #1 and #2 are headers. Topic #2 should be "N !", and topic 
  1446. #1's left pointer should be N.
  1447.                 30 IF LOF(1)=0 THEN N=2: LSET TF$="N !": LSET 
  1448. PF$(1)=Z$: LSET PF$(2)=Z$: PUT 1,2 ELSE GET 1,1: N=CV
  1449.                 I(PF$(1))
  1450. Ask the human for a topic.40 PRINT: PRINT "WHAT TOPIC INTERESTS 
  1451. YOU?": PRINT "(IF YOU'RE NOT SURE, TYPE A QUESTION MARK)": PR
  1452.                 INT "(IF YOU WANT TO END, TYPE THE LETTER X)": 
  1453. LINE INPUT T$
  1454. If the human wishes, do 1000.50 IF T$="?" THEN GOSUB 1000: GO TO 
  1455. 40
  1456. If the human wishes, end.60 IF T$="X" THEN LSET PF$(1)=MKI$(N): 
  1457. PUT 1,1: CLOSE: END
  1458. Make TS$ resemble T$ but be 44 characters long (by adding blank 
  1459. spaces at the end).
  1460.                 70 LSET TF$=T$: TS$=TF$
  1461. Find the topic. 80 GOSUB 2000
  1462. Go to another topic.90 GO TO 40
  1463.  
  1464. Subroutine 1000D: tell the human what topics are in the file
  1465. If just headers, say so.1000 IF N=2 THEN PRINT "I DON'T KNOW ANY 
  1466. TOPICS YET.": PRINT "MY MIND IS STILL BLANK.": PRINT "PLEA
  1467.                 SE TEACH ME A NEW TOPIC.": RETURN
  1468. If file has topics, list them.1010 PRINT "I KNOW ABOUT THESE 
  1469. TOPICS:": FOR I = 3 TO N: GET 1,I: PRINT TF$: NEXT: PRINT "PICK 
  1470. ONE 
  1471.                 OF THOSE TOPICS, OR TEACH ME A NEW ONE."
  1472.                 1020 RETURN
  1473.  
  1474. Subroutine 2000D: search through the file, to find the topic TS$
  1475. Starting at 2, hunt for TS$.2000 I=2: GOSUB 8000
  1476. If absent, do 4000, else 3000.2010 IF I=0 THEN GOSUB 4000 ELSE 
  1477. GOSUB 3000
  1478.                 2020 RETURN
  1479.  
  1480. Subroutine 3000D: the topic's in the file
  1481. Tell the human about the topic.3000 PRINT "HERE'S WHAT I KNOW 
  1482. ABOUT "T$":": PRINT DF$
  1483. Ask whether to change the info.3010 INPUT "DO YOU WANT TO CHANGE 
  1484. THAT INFORMATION";A$
  1485. If the human says yes, do 5000.3020 IF A$="YES" OR A$="Y" THEN 
  1486. GOSUB 5000
  1487.                 3030 RETURN
  1488.  
  1489. Subroutine 4000D: the topic's not in the file
  1490. Say topic is not in the file.4000 PRINT "I DON'T KNOW ANYTHING 
  1491. ABOUT "T$"."
  1492. Request info about the topic.4010 PRINT: PRINT "TELL ME ABOUT 
  1493. "T$: PRINT "(IF YOU DON'T WANT TO TELL ME, TYPE THE LETTER X)": 
  1494. LI
  1495.                 NE INPUT D$
  1496. If human wants, insert topic.4020 IF D$<>"X" THEN GOSUB 6000
  1497.                 4030 RETURN
  1498.  
  1499. Subroutine 5000D: change the info
  1500. Agree to change the info.5000 PRINT "OKAY. I'VE ERASED THAT 
  1501. INFORMATION ABOUT "T$"."
  1502. Request new info.5010 PRINT: PRINT "TYPE WHAT YOU WANT ME TO KNOW 
  1503. ABOUT "T$: PRINT "(IF YOU WANT ME TO FORGET "T$", 
  1504.                 TYPE THE LETTER X)": LINE INPUT D$
  1505. Change the info as requested.5020 IF D$="X" THEN GOSUB 7000 ELSE 
  1506. LSET DF$=D$: PUT 1,I
  1507.                 5030 RETURN
  1508.  
  1509. Subroutine 6000D: insert the topic into the file
  1510. Increase the number of topics.6000 N=N+1
  1511. Let topic I1 point to topic N.6010 LSET PF$(J)=MKI$(N): PUT 1,I1
  1512. Append the new topic, at N.6020 LSET TF$=TS$: LSET DF$=D$: LSET 
  1513. PF$(1)=Z$: LSET PF$(2)=Z$: PUT 1,N
  1514.                 6030 RETURN
  1515.  
  1516. Subroutine 7000D: delete the topic from the file
  1517. A is the number of the vanishing topic. P1 and P2 are the topic's 
  1518. pointers.
  1519.                 7000 A=I: P1=CVI(PF$(1)): P2=CVI(PF$(2))
  1520. Make topic I1 (which was pointing to the vanishing topic) point 
  1521. to the vanishing topic's left child instead.
  1522.                 7010 GET 1,I1: LSET PF$(J)=MKI$(P1): PUT 1,I1
  1523. If the vanishing topic has a right child, make something point to 
  1524. that child.
  1525.                 7020 IF P2>0 THEN GET 1,P2: TS$=TF$: I=I1: GOSUB 
  1526. 8000: LSET PF$(J)=MKI$(P2): PUT 1,I1
  1527. If the vanishing topic isn't topic N, move topic N to the gap 
  1528. left by the vanishing topic.
  1529.                 7030 IF A<N THEN GET 1,N: PUT 1,A: TS=TF$: I=2: 
  1530. GOSUB 8000: GET 1,I1: LSET PF$(J)=MKI$(A): PUT 1,I1
  1531. Decrease the number of topics.7040 N=N-1
  1532.                 7050 RETURN
  1533.  
  1534. Subroutine 8000D
  1535. Starting at position I, hunt for TS$ in the file. If TS$ is 
  1536. missing from the database, make I be 0; otherwise make I be the 
  1537. position of TS$.
  1538. Find the topic that points to TS$. Make I1 be that topic's 
  1539. position. If the arrow pointing to TS$ points left, make J be 1; 
  1540. otherwise make J be 2.
  1541. Here's how to do all that:
  1542.  
  1543. Start at position I.8000 GET 1,I
  1544. If the topic is found, return.8010 IF TS$=TF$ THEN RETURN
  1545. Compute a tentative J.8020 IF TS$<TF$ THEN J=1 ELSE J=2
  1546. Compute a tentative I1 and I.8030 I1=I: I=CVI(PF$(J))
  1547. If pointer not 0, hunt again.8040 IF I>0 THEN GO TO 8000
  1548.                 8050 RETURN